//
//  BaseTabbarViewController.m
//  Codingke2016
//
//  Created by xujiangtao on 16/5/30.
//  Copyright © 2016年 xujiangtao. All rights reserved.
//

#import "BaseTabbarViewController.h"
#import "BaseNaviViewController.h"
#import "HomeViewController.h"
#import "CourseViewController.h"
#import "CommunityViewController.h"
#import "MyViewController.h"

@interface BaseTabbarViewController ()<UITabBarControllerDelegate>

@end

@implementation BaseTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    [self initTabbar];
}

#pragma mark- initTabBar
- (void)initTabbar{
    
    [[UITabBar appearance] setBackgroundImage:[UIImage imageNamed:@"img_icon_tab_bg"]];
    
    //去掉tabbar上面的黑线
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [[UINavigationBar appearance] setBarTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setTintColor:RGBACOLOR(0, 175, 86, 1)];
//    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    
    HomeViewController* homeViewController = [[HomeViewController alloc] init];
    CourseViewController* courseViewController = [[CourseViewController alloc] init];
    CommunityViewController* communityViewController = [[CommunityViewController alloc] init];
    MyViewController* myViewController = [[MyViewController alloc] init];
    
    homeViewController.title = @"首页";
    courseViewController.title = @"课程";
    communityViewController.title = @"社区";
    myViewController.title = @"我的";
    
    UIImage* homeIconImage = [UIImage imageNamed:@"img_home_tab"];
    UIImage* courseIconImage = [UIImage imageNamed:@"img_course_tab"];
    UIImage* communityIconImage = [UIImage imageNamed:@"img_community_tab"];
    UIImage* myIconImage = [UIImage imageNamed:@"img_my_tab"];
    
    UIImage* homeClickedIconImage = [[UIImage imageNamed:@"img_home_tab_clicked"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage* courseClickedIconImage = [[UIImage imageNamed:@"img_course_tab_clicked"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage* communityClickedIconImage = [[UIImage imageNamed:@"img_community_tab_clicked"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage* myClickedIconImage = [[UIImage imageNamed:@"img_my_tab_clicked"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    
    UIEdgeInsets tabbarInsets = UIEdgeInsetsMake(7, 0, -7, 0);

    homeViewController.tabBarItem.image = homeIconImage;
    homeViewController.tabBarItem.selectedImage = homeClickedIconImage;
    homeViewController.tabBarItem.imageInsets = tabbarInsets;
    homeViewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 50);

    courseViewController.tabBarItem.image = courseIconImage;
    courseViewController.tabBarItem.selectedImage = courseClickedIconImage;
    courseViewController.tabBarItem.imageInsets = tabbarInsets;
    courseViewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 50);
    
    communityViewController.tabBarItem.image = communityIconImage;
    communityViewController.tabBarItem.selectedImage = communityClickedIconImage;
    communityViewController.tabBarItem.imageInsets = tabbarInsets;
    communityViewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 50);
    
    myViewController.tabBarItem.image = myIconImage;
    myViewController.tabBarItem.selectedImage = myClickedIconImage;
    myViewController.tabBarItem.imageInsets = tabbarInsets;
    myViewController.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, 50);
    
    
    BaseNaviViewController* homeNaviViewController = [[BaseNaviViewController alloc] initWithRootViewController:homeViewController];
    BaseNaviViewController* courseNaviViewControler = [[BaseNaviViewController alloc] initWithRootViewController:courseViewController];
    BaseNaviViewController* communityNaviViewController = [[BaseNaviViewController alloc] initWithRootViewController:communityViewController];
    BaseNaviViewController* myNaviViewController = [[BaseNaviViewController alloc] initWithRootViewController:myViewController];
    
    [self setViewControllers:@[homeNaviViewController,courseNaviViewControler,communityNaviViewController,myNaviViewController]];
}
#pragma mark- UITabbarControllerDelegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    BOOL result = YES;
    
    if (viewController == tabBarController.viewControllers[3]) {
        
        [UtilsManager loginInViewController:self completion:nil];
        result = NO;
    }
    return result;
}

#pragma mark-
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
